package Week.w151;

import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class Solution1 {

    Map<String,List<String[]>> map=new HashMap<>();
    public List<String> invalidTransactions(String[] transactions) {

        List<String> result=new ArrayList<>();
        for(String s:transactions){
            String[] strArray=s.split(",");
            if(Integer.parseInt(strArray[2])>1000){
                result.add(s);
                if(map.containsKey(strArray[0])) {
                    List<String[]> list = map.get(strArray[0]);
                    list.add(strArray);
                }else {
                    List<String[]> list=new ArrayList<>();
                    list.add(strArray);
                    map.put(strArray[0],list);
                }
            }
            if(map.containsKey(strArray[0])){
                List<String[]> list=map.get(strArray[0]);
                List<String> temp=getTempResult( strArray,list);
                list.add(strArray);
                result.addAll(temp);
            }else {
                List<String[]> list=new ArrayList<>();
                list.add(strArray);
                map.put(strArray[0],list);
            }
        }

        return  result.stream().distinct().collect(Collectors.toList());
    }

    private List<String> getTempResult( String[] strArray,List<String[]> list){
        List<String> tempList=new ArrayList<>();
        for(String[] strings:list){
            int a=Integer.parseInt(strings[1]);
            int b=Integer.parseInt(strArray[1]);
            if(Math.abs(a-b)<=60&&!strings[3].equals(strArray[3])){
                tempList.add(String.join(",",strings));
                tempList.add(String.join(",",strArray));
            }
        }
        return tempList;

    }


   @Test
    public void test() {
       String[] arrays = new String[]{"bob,689,1910,barcelona", "alex,696,122,bangkok", "bob,832,1726,barcelona", "bob,820,596,bangkok", "chalicefy,217,669,barcelona", "bob,175,221,amsterdam"};

       invalidTransactions(arrays);

   }
}
